/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.synjones.pay.bank.ecard.service;

import com.alibaba.fastjson.JSON;
import com.synjones.pay.bank.api.AbstractBankDirectorImpl;
import com.synjones.pay.bank.ecard.bean.WxApiStr;
import com.synjones.pay.bank.ecard.bean.YktApiParaName;
import com.synjones.pay.bank.wx.bean.common.ExtendedServApiUrl;
import com.synjones.pay.bank.wx.bean.common.WxResponseBean;
import com.synjones.pay.bank.wx.bean.transfer.YktTransferReqBean;
import java.util.HashMap;
import java.util.Map;

import com.synjones.pay.bank.wx.helper.ExtendedServHelper;

import com.synjones.pay.model.common.CommonConstant;
import com.synjones.pay.model.common.ResponseBean;
import com.synjones.pay.model.common.ResponseBean.Builder;
import com.synjones.pay.model.context.IPayContext;
import com.synjones.pay.model.context.PayRequest;
import com.synjones.pay.model.transfer.TransferTradeBean;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @author liubinqiang
 * @date 2017-5-18 13:51:38
 * @version V1.0
 * @desc 校园卡账户间转账
 */
public class EcardDirectorImpl extends AbstractBankDirectorImpl {

    private final static Logger logger = LoggerFactory.getLogger(EcardDirectorImpl.class);

    @Override
    public Map<String, String> sendData(Map<String, String> reqData) {

        //组装转账bean
        YktTransferReqBean transferBean = getYktTransferReqBean(reqData);

        String responseStr = ExtendedServHelper.postToServer(ExtendedServApiUrl.CARD_TRANSFER, JSON.toJSONString(transferBean));

        logger.info("微校响应={}", responseStr);
        Map<String, String> respMap = new HashMap<>();
        respMap.put(WxApiStr.RESPONSE, responseStr);
        return respMap;
    }

    /*
    {"errCode":0,
    "errMsg":null,
    "time":"2017-05-18T20:27:45.9779246+08:00",
    "request":{"tranamt":1.0,"src_account":"1","dst_account":"1","acctype":"###","remark":null,"msg":null},
    "response":{"errCode":0,"errMsg":null,"tipMsg":null,"data":{"retcode":60003,"errmsg":"账户信息不存在"},"time":"2017-05-18T20:27:46.4771255+08:00"},
    "lcient":null,
    "user":{"ticket":null,"uid":null,"orgCode":null,"sysCode":null,"sessionId":null},
    "secret":{"ip":null,"realm":null,"accessToken":null,"nonce":"52c3679d9e8b4167a9927ff77674396b","timestamp":"2017-05-18T20:27:45.9779246+08:00","signature":"da1dfb24373d8672931416891989f51e52e6fe4e"},"signature":"04ad445ef3b4501a93977cc0b73354707287aa44"}
        
     */
    /**
     * 讲请求map 转换为请求对象
     *
     * @param reqData
     * @return
     */
    private YktTransferReqBean getYktTransferReqBean(Map<String, String> reqData) {
        YktTransferReqBean transferBean = new YktTransferReqBean();
        //金额单位（分）
        transferBean.setTranamt(reqData.get(YktApiParaName.TRANAMT));
        transferBean.setSrc_account(reqData.get(YktApiParaName.SRC_ACCOUNT));
        transferBean.setDst_account(reqData.get(YktApiParaName.DST_ACCOUNT));
        transferBean.setAcctype(reqData.get(YktApiParaName.ACCTYPE));
        transferBean.setRemark(reqData.get(YktApiParaName.REMARK));
        return transferBean;
    }

    /**
     * 生成转账请求信息
     *
     * @param ctx
     * @return
     */
    @Override
    public Map<String, String> buildSendData(IPayContext ctx) {
        //从ctx获取转账请求信息
        PayRequest req = ctx.getRequest();
        TransferTradeBean reqBean = req.get(CommonConstant.TRADE_OBJ);
        Map<String, String> sendDataMap = new HashMap<>();
        sendDataMap.put(YktApiParaName.TRANAMT, reqBean.getAmount());
        sendDataMap.put(YktApiParaName.SRC_ACCOUNT, reqBean.getRolloutAcc());
        sendDataMap.put(YktApiParaName.DST_ACCOUNT, reqBean.getRollinAcc());
        sendDataMap.put(YktApiParaName.ACCTYPE, "###");
        sendDataMap.put(YktApiParaName.REMARK, reqBean.getOrderId());
        return sendDataMap;
    }

    /**
     * 解析返回结果
     *
     * @param resMap
     * @return
     */
    @Override
    public ResponseBean resolveRespData(Map<String, String> resMap) {

        String respStr = resMap.get(WxApiStr.RESPONSE);
        WxResponseBean responce = JSON.parseObject(respStr, WxResponseBean.class);

        logger.info("转账-yktTransResp={}", responce.getErrMsg());
        Builder builder = new Builder();
        builder.statusCode(responce.getErrCode());
        builder.orderNo(responce.getErrMsg());
        ResponseBean responseBean = builder.builder();
        return responseBean;
    }

    /**
     * 验证签名
     *
     * @param respMap
     * @return
     */
    @Override
    public boolean verifyRespSign(Map<String, String> respMap) {
        String resp = respMap.get(WxApiStr.RESPONSE);
        return resp != null;
    }

}
